package com.ruoyi.wechat.domain;

import com.alibaba.fastjson2.annotation.JSONField;
import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableName;
import com.baomidou.mybatisplus.extension.activerecord.Model;
import com.fasterxml.jackson.annotation.JsonFormat;
import com.ruoyi.common.utils.Json;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import lombok.EqualsAndHashCode;
import me.chanjar.weixin.common.bean.WxOAuth2UserInfo;
import me.chanjar.weixin.mp.bean.result.WxMpUser;
import org.springframework.util.StringUtils;

import java.util.Date;

/**
 * @author yan
 * @Data 2021-11-04
 */
@Data
@TableName("wx_user")
@EqualsAndHashCode()
public class WxUser extends Model<WxUser> {
    private static final long serialVersionUID = 1L;
    @ApiModelProperty(name = "openId", value = "openId")
    @TableId(type = IdType.INPUT)
    private String openid;
    @ApiModelProperty(name = "appid", value = "appid")
    private String appid;
    @ApiModelProperty(name = "phone", value = "手机号")
    private String phone;
    @ApiModelProperty(name = "nickname", value = "昵称")
    private String nickname;
    @ApiModelProperty(name = "sex", value = "性别(0-未知、1-男、2-女)")
    private String sex;
    @ApiModelProperty(name = "city", value = "城市")
    private String city;
    @ApiModelProperty(name = "province", value = "地区")
    private String province;
    @ApiModelProperty(name = "headimgurl", value = "头像地址")
    private String headimgurl;
    @ApiModelProperty(name = "subscribeTime", value = "关注时间")
    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
    @JSONField(name = "subscribe_time")
    private Date subscribeTime;
    /**
     * 是否关注
     */
    private String subscribe;
    private String unionid;
    /**
     * 备注信息
     */
    private String remark;
    /**
     * 标签ID集合
     */
    private String tagidList;
    @TableField(exist = false)
    private String[] tagIds;
    /**
     * 关注场景
     */
    private String subscribeScene;
    private String qrSceneStr;

    public WxUser() {
    }

    public String[] getTagIds() {
        if (com.ruoyi.common.utils.StringUtils.isNotEmpty(getTagidList())) {
            tagIds = getTagidList().split(",");
        }
        return tagIds;
    }

    public WxUser(String openid) {
        this.openid = openid;
    }

    public WxUser(WxMpUser wxMpUser, String appid) {
        this.openid = wxMpUser.getOpenId();
        this.appid = appid;
        Boolean subscribe = wxMpUser.getSubscribe();
        if (subscribe) {
            this.subscribeTime = new Date(wxMpUser.getSubscribeTime() * 1000);
            this.unionid = wxMpUser.getUnionId();
            this.remark = wxMpUser.getRemark();
            this.subscribe = "1";
            this.tagidList = com.ruoyi.common.utils.StringUtils.join(wxMpUser.getTagIds(), ",");
            this.subscribeScene = wxMpUser.getSubscribeScene();
            String qrScene = wxMpUser.getQrScene();
            this.qrSceneStr = !StringUtils.hasText(qrScene) ? wxMpUser.getQrSceneStr() : qrScene;
        }
    }

    public WxUser(WxOAuth2UserInfo wxMpUser, String appid) {
        this.openid = wxMpUser.getOpenid();
        this.appid = appid;
        if (wxMpUser.getNickname() != null) {
            this.nickname = wxMpUser.getNickname();
            this.headimgurl = wxMpUser.getHeadImgUrl();
            this.unionid = wxMpUser.getUnionId();
        }
    }

    @Override
    public String toString() {
        return Json.toJsonString(this);
    }

}
